package com.qf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sys.po.Dept;
import com.qf.sys.po.Log;
import com.qf.sys.po.Module;
import com.qf.sys.po.Role;
import com.qf.sys.service.RoleService;
import com.qf.sys.util.LayUIOperate;
import com.qf.sys.util.Msg;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/8
 * @描述
 */
@Controller
@RequestMapping("role")
public class RoleController {
    @Resource
    RoleService roleService;


    @RequestMapping("selectRoleList")
    @ResponseBody
    public Map selectRoleList(){//查询部门信息
        Map<String,Object> maps = new HashMap<>();
        List<Role> roleList = roleService.selectRoleList();//查询出数据
        System.out.println(roleList);
        maps.put("data",roleList);
        return maps;
    }
    @RequestMapping("getRoleList")
    @ResponseBody
    public Msg getRoleList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String _deptNo = request.getParameter("deptNo");
        int deptNo = 0;
        if (_deptNo != null && !"".equals(_deptNo)) {
            deptNo = Integer.parseInt(_deptNo);
        }
        String job = request.getParameter("job");
        String _status = request.getParameter("status");
        System.out.println(_status);
        int status = 3;
        if (_status != null && !"".equals(_status)) {
            status = Integer.parseInt(_status);
        }
        Map params = new HashMap();
        params.put("job", job);
        params.put("deptNo", deptNo);
        params.put("status", status);
        System.out.println(status);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Role> data = roleService.getRoleList(params);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("处理成功");
        msg.setCount(data.getTotal());//得到总条数
        msg.setData(data.getList());//得到查出的List数据
        System.out.println(msg);
        return msg;
    }
    @RequestMapping("addRole")
    @ResponseBody
    public LayUIOperate addRole(@RequestBody Map<String,Object> params){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("addRole -->"+params);
        boolean f= roleService.addRole(params);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("职位添加成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("职位添加失败");
        }
        return operate;
    }
    @RequestMapping("updateRole")
    @ResponseBody
    public LayUIOperate updateRole(@RequestBody Map<String,Object> params){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("updateRole -->"+params);
        boolean f= roleService.updateRole(params);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("职位修改成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("职位修改失败");
        }
        return operate;
    }

    @RequestMapping("deleteRole")
    @ResponseBody
    public LayUIOperate deleteRole(Integer id){
        LayUIOperate operate=new LayUIOperate();
        boolean f= roleService.deleteRole(id);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("职位删除成功！");
        }else{
            operate.setSuccess(false);
            operate.setMessage("职位删除失败！");
        }
        return operate;
    }
    @RequestMapping("addRoleAuth")
    @ResponseBody
    public LayUIOperate addRoleAuth(int roleId,int[] moduleIds){
        LayUIOperate operate=new LayUIOperate();
        //String  roleId = request.getParameter("roleId");
       /* int roleid = 0;
        if (roleId != null && !"".equals(roleId)) {
            roleid = Integer.parseInt(roleId);
        }*/
        System.out.println(roleId);
        System.out.println(moduleIds);
        boolean f= roleService.addRoleAuth(roleId,moduleIds);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("职位赋权成功！");
        }else{
            operate.setSuccess(false);
            operate.setMessage("职位赋权失败！");
        }
        return operate;
    }

    @RequestMapping("upadteRolestatusTo0")
    @ResponseBody
    public LayUIOperate upadteUserstatusTo0(int id){
        LayUIOperate operate=new LayUIOperate();
        boolean f= roleService.upadteRolestatusTo0(id);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
    @RequestMapping("upadteRolestatusTo1")
    @ResponseBody
    public LayUIOperate upadteDeptstatusTo1(int id){
        LayUIOperate operate=new LayUIOperate();
        boolean f= roleService.upadteRolestatusTo1(id);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
}
